package com.isky.visual.db.controller;


import com.alibaba.fastjson.JSONObject;
import com.isky.visual.constant.CommonConstant;
import com.isky.visual.db.entity.Datastore;
import com.isky.visual.db.entity.GenericSqlModel;
import com.isky.visual.db.service.IDatastoreService;
import com.isky.visual.db.vo.QueryDataStoreParam;
import com.isky.visual.db.vo.ReportModel;
import com.isky.visual.interceptor.PlatformUserManager;
import com.isky.visual.interceptor.annotation.LoginValidate;
import com.isky.visual.result.CodeMsg;
import com.isky.visual.result.ResultVo;
import com.isky.visual.user.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 结果集 的展示
 * </p>
 *
 * @author wudl
 * @since 2020-04-23
 */
@Slf4j
@RestController
@RequestMapping("/db/datastore")
@Api(tags = {"结果数据集接口"})
public class DatastoreController {

    @Autowired
    IDatastoreService datastoreService;

    @PostMapping(value = "/getAllDataStore")
    public ResultVo<List<Datastore>>   getAllDataStore(@RequestBody Map<String, Long>  mapdb)
    {
        ResultVo<List<Datastore>> allDataStore = datastoreService.getAllDataStore();
        return allDataStore;
    }

    @LoginValidate(false)
    @ApiOperation("获取数据源")
    @PostMapping(value = "/getDataStore")
    public ResultVo<ReportModel>  getIdDataStore(HttpServletRequest request, @RequestBody QueryDataStoreParam queryDataStoreParam)
    {
        String env = request.getHeader("env");
        log.info("===================================:" + env);

        if (StringUtils.isBlank(env)) {
            User user = PlatformUserManager.getUser();
        }

        ResultVo<ReportModel> reportModelResultVo = datastoreService.showDataStore(queryDataStoreParam.getId(),queryDataStoreParam.getType());
        return reportModelResultVo;
    }

    @PostMapping(value = "/getUsedDataStore")
    @ApiOperation("根据用户id查询数据集")
    @ApiImplicitParams({

    })
    public ResultVo<List<Datastore>> getUsedDataStore()
    {
        User user = PlatformUserManager.getUser();
        return datastoreService.getUsedDataStore(user.getId());
    }
    @PostMapping(value = "/callDataStore")
    @ApiOperation("根据用户id和数据集名称查找出来数据集做一个回显")
    @ApiImplicitParams({

            @ApiImplicitParam(name = "datastoreid", value = "结果集id", required = true, dataType = "Long", paramType = "query")
    })
    public ResultVo<List<Datastore>> callDataStore(@RequestParam(name = "datastoreid" ,required = true)Long datastoreid)
    {
        return  datastoreService.getUsedDataStore(datastoreid);
    }

    @PostMapping(value = "/deleteDataStore")
    @ApiOperation("删除数据集")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "数据集id", required = true, dataType = "Long", paramType = "query")
    })
    public ResultVo<String> deleteDataStore(@RequestParam(name = "id" ,required = true)Long id)
    {
        return  datastoreService.deleteDataStore(id);
    }
    @PostMapping(value = "/updateDataStore")
    @ApiOperation("更新数据集")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "数据集id", required = true, dataType = "Long", paramType = "query")
    })
    public ResultVo<Object> updateDataStore(@RequestBody Datastore  datastore)
    {
        try {
            String jsons = datastore.getJsons();
            if (StringUtils.isBlank(jsons)) {
                datastore.setJsons(null);
            } else {
                JSONObject.parseObject(datastore.getJsons());
            }
        } catch (Exception e) {
            return ResultVo.error(CodeMsg.DATA_FORMATTED_ERROR);
        }

        return  datastoreService.updateDataStore(datastore);
    }



}
